using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BUS;
using Entities;
using Utility;
using System.IO;

public partial class Admin_SanPhamUpdate : System.Web.UI.Page
{
    private void LoadDataHinh()
    {
        int id = int.Parse(Request.QueryString["id"].ToString());
        Criteria cri = new Criteria();
        int total = 0;
        cri.add(Condition.AND, tbHinhAnh.tbHinhAnhColumns.IDSanPham, Condition.EQUAL, int.Parse(id.ToString()));

        tbHinhAnh.tbHinhAnhCollection hacollection = tbHinhAnhBus.Get(cri.Criter, Pager.CurrentIndex, Pager.PageSize,
        tbHinhAnh.tbHinhAnhColumns.IDHinhAnh.ToString(), OrderDirection.ASC.ToString(), out total);
        Pager.PageSize = 5;

        Repeater2.DataSource = hacollection;
        Repeater2.DataBind();
        Pager.ItemCount = total;
        if (total <= Pager.PageSize)
            Pager.Visible = false;
    }
    private void LoadDropDownList()
    {
        tbLoaiSP.tbLoaiSPCollection loai_sp_collection = tbLoaiSPBUS.GetAll();
        ddl_loai.DataTextField = tbLoaiSP.tbLoaiSPColumns.TenLoaiSP.ToString();
        ddl_loai.DataValueField = tbLoaiSP.tbLoaiSPColumns.IDLoaiSP.ToString();
        ddl_loai.DataSource = loai_sp_collection;
        ddl_loai.DataBind();

        tbNhanVien.tbNhanVienCollection nv_collection = tbNhanVienBUS.GetAll();
        ddl_nv.DataTextField = tbNhanVien.tbNhanVienColumns.TenNV.ToString();
        ddl_nv.DataValueField = tbNhanVien.tbNhanVienColumns.IDNhanVien.ToString();
        ddl_nv.DataSource = nv_collection;
        ddl_nv.DataBind();

        tbDoanhNghiep.tbDoanhNghiepCollection dn_collection = tbDoanhNghiepBus.GetAll();
        ddl_dn.DataTextField = tbDoanhNghiep.tbDoanhNghiepColumns.TenDoanhNghiep.ToString();
        ddl_dn.DataValueField = tbDoanhNghiep.tbDoanhNghiepColumns.IDDoanhNghiep.ToString();
        ddl_dn.DataSource = dn_collection;
        ddl_dn.DataBind();

        tbTrangThaiSP.tbTrangThaiSPCollection ttsp_collection = tbTrangThaiSPBUS.GetAll();
        ddl_trangthai.DataTextField = tbTrangThaiSP.tbTrangThaiSPColumns.TrangThai.ToString();
        ddl_trangthai.DataValueField = tbTrangThaiSP.tbTrangThaiSPColumns.IDTrangThaiSP.ToString();
        ddl_trangthai.DataSource = ttsp_collection;
        ddl_trangthai.DataBind();

        tbHangSanXuat.tbHangSanXuatCollection hsx_collection = tbHangSanXuatBUS.GetAll();
        ddl_hsx.DataTextField = tbHangSanXuat.tbHangSanXuatColumns.TenHangSanXuat.ToString();
        ddl_hsx.DataValueField = tbHangSanXuat.tbHangSanXuatColumns.IDHangSanXuat.ToString();
        ddl_hsx.DataSource = hsx_collection;
        ddl_hsx.DataBind();

        tbDiaDiem.tbDiaDiemCollection dd_collection = tbDiaDiemBUS.GetAll();
        ddl_diadiem.DataTextField = tbDiaDiem.tbDiaDiemColumns.DiaDiem.ToString();
        ddl_diadiem.DataValueField = tbDiaDiem.tbDiaDiemColumns.IDDiaDiem.ToString();
        ddl_diadiem.DataSource = dd_collection;
        ddl_diadiem.DataBind();

        try
        {
            int id = int.Parse(Request.QueryString["id"].ToString());
            tbSanPham sp = tbSanPhamBus.GetByID(id);
            if (sp != null)
            {
                txt_masp.Text = id.ToString();
                txt_tensp.Text = sp.TenSanPham.ToString();
                tbLoaiSP loai = tbLoaiSPBUS.GetByID(sp.IDLoaiSP);
                ddl_loai.SelectedValue = loai.TenLoaiSP.ToString();
                tbDiaDiem dd = tbDiaDiemBUS.GetByID(sp.IDDiaDiem);
                ddl_diadiem.SelectedValue = dd.DiaDiem.ToString();
                tbHangSanXuat hsx = tbHangSanXuatBUS.GetByID(sp.IDHangSanXuat);
                ddl_hsx.SelectedValue = hsx.TenHangSanXuat.ToString();
                tbTrangThaiSP trangthai = tbTrangThaiSPBUS.GetByID(sp.IDTrangThaiSP);
                ddl_trangthai.SelectedValue = trangthai.TrangThai.ToString();
                tbNhanVien nv = tbNhanVienBUS.GetByID(sp.IDNhanVien);
                ddl_nv.SelectedValue = nv.TenNV.ToString();
                tbDoanhNghiep dn = tbDoanhNghiepBus.GetByID(sp.IDDoanhNghiep);
                ddl_dn.SelectedValue = dn.TenDoanhNghiep.ToString();
                //txt_dongiaban.Text = sp.GiaBan.ToString();
                //txt_dgthuc.Text = sp.GiaTriThuc.ToString();
               // txt_dksd.Text = sp.DieuKienSuDung.ToString();
                //txt_mota.Text = sp.MoTaSP.ToString();
                //txt_sl_sp.Text = sp.SoLuongSP.ToString();
               // txt_slban.Text = sp.SoLuongBan.ToString();
               // txt_sl_daban.Text = sp.SoLuongDaBan.ToString();
                //txt_ngaybdban.Text = sp.NgayBDBan.ToString("dd/MM/yyyy");
                //txt_ngay_kt.Text = sp.NgayKTBan.ToString("dd/MM/yyyy");
                //txt_ngaycapnhat.Text = sp.NgayCapNhat.ToString("dd/MM/yyyy");
                //txt_tlhh.Text = sp.TiLeHoaHong.ToString();
             
            }
        }
        catch
        {
            Response.Redirect("SanPhamList.aspx");
        }
        LoadDataHinh();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadDropDownList();
            LoadDataHinh();
        }

    }

    protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item
              || e.Item.ItemType == ListItemType.AlternatingItem)
        {

            Literal lit_id_hinh = (Literal)e.Item.FindControl("lit_id_hinh");
            HiddenField hf_id = (HiddenField)e.Item.FindControl("hf_id");
            HiddenField hf_tenhinh = (HiddenField)e.Item.FindControl("hf_tenhinh");

            Image img_HinhAnh = (Image)e.Item.FindControl("img_HinhAnh");
            tbHinhAnh ha = (tbHinhAnh)e.Item.DataItem;
            img_HinhAnh.ImageUrl = "~/Admin/Photos/Thumb/" + ha.TenHinh;

            lit_id_hinh.Text = ha.IDHinhAnh.ToString();
            hf_id.Value = lit_id_hinh.Text;
            hf_tenhinh.Value = ha.TenHinh.ToString();

        }
    }
    protected void Pager_Command(object sender, CommandEventArgs e)
    {
        Pager.CurrentIndex = int.Parse(e.CommandArgument.ToString());
        LoadDataHinh();
    }

   

    protected void lbt_Xoa_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in this.Repeater2.Items)
        {
            CheckBox chk = (CheckBox)item.FindControl("cb_Xoa");
            if (chk.Checked == true)
            {
                HiddenField hf_tenhinh = (HiddenField)item.FindControl("hf_tenhinh");
                File.Delete(Server.MapPath("~/Admin/Photos/") + hf_tenhinh.Value);
                File.Delete(Server.MapPath("~/Admin/Photos/Thumb/") + hf_tenhinh.Value);
                HiddenField hf_id = (HiddenField)item.FindControl("hf_id");
                tbHinhAnhBus.Delete_HinhAnh(int.Parse(hf_id.Value));
            }
        }
        LoadDataHinh();
       
    }
   
    protected void bt_update_Click(object sender, EventArgs e)
    {
        try
        {
            tbSanPham sp = tbSanPhamBus.GetByID(int.Parse(txt_masp.Text));
            sp.TenSanPham = txt_tensp.Text;
            sp.IDLoaiSP = int.Parse(ddl_loai.SelectedValue);
            sp.IDDiaDiem = int.Parse(ddl_diadiem.SelectedValue);
            sp.IDHangSanXuat = int.Parse(ddl_hsx.SelectedValue);
            sp.IDTrangThaiSP = int.Parse(ddl_trangthai.SelectedValue);
            sp.IDNhanVien = int.Parse(ddl_nv.SelectedValue);
            sp.IDDoanhNghiep = int.Parse(ddl_dn.SelectedValue);
            sp.GiaBan = int.Parse(txt_dongiaban.Text);
            sp.GiaTriThuc = int.Parse(txt_dgthuc.Text);
            sp.DieuKienSuDung = txt_dksd.Text;
            sp.MoTaSP = txt_mota.Text;
            sp.SoLuongSP = int.Parse(txt_sl_sp.Text);
            sp.SoLuongBan = int.Parse(txt_slban.Text);
            sp.SoLuongDaBan = int.Parse(txt_sl_daban.Text);
            sp.NgayBDBan = DateTime.Parse(txt_ngaybdban.Text);
            sp.NgayKTBan = DateTime.Parse(txt_ngay_kt.Text);
            sp.NgayCapNhat = DateTime.Parse(txt_ngaycapnhat.Text);
            sp.TiLeHoaHong = int.Parse(txt_tlhh.Text);

            tbSanPhamBus.Update_SanPham (sp);


            HttpFileCollection hfc = Request.Files;
            for (int i = 0; i < hfc.Count; i++)
            {
                HttpPostedFile hpf = hfc[i];
                if (hpf.ContentLength > 0)
                {
                    string filename = DateTime.Now.Ticks.ToString() + "_" + Path.GetFileName(hpf.FileName);
                    hpf.SaveAs(Server.MapPath("~/Admin/Photos/" + filename));

                    System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("~/Admin/Photos/" + filename));

                    ImageUtility.MakeThumbnailPhoto(img, 100, 100, Server.MapPath("~/Admin/Photos/Thumb/"), filename);

                    tbHinhAnh ha = new tbHinhAnh();
                    ha.IDSanPham = int.Parse(txt_masp.Text);
                    ha.DuongDan = filename;
                    ha.TenHinh = filename;
                    
                    tbHinhAnhBus.Insert_HinhAnh (ha);

                }
            }
        }
        catch
        {
            Response.Redirect("SanPhamList.aspx");
        }
    }
}
